/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.LotteryConfig;
import com.boxin.ims.modules.wechat.service.LotteryConfigService;

/**
 * 抽奖配置Controller
 * @author Jakemanse
 * @version 2013-09-30
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/lotteryConfig")
public class LotteryConfigController extends BaseController {

	@Autowired
	private LotteryConfigService lotteryConfigService;
	
	@ModelAttribute
	public LotteryConfig get(@RequestParam(required=false) Long id) {
		if (id != null){
			return lotteryConfigService.get(id);
		}else{
			return new LotteryConfig();
		}
	}
	
	@RequiresPermissions("wechat:lotteryConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(LotteryConfig lotteryConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			lotteryConfig.setUser(user);
		}
        Page<LotteryConfig> page = lotteryConfigService.find(new Page<LotteryConfig>(request, response), lotteryConfig); 
        model.addAttribute("page", page);
		return "modules/wechat/lotteryConfigList";
	}

	@RequiresPermissions("wechat:lotteryConfig:view")
	@RequestMapping(value = "form")
	public String form(LotteryConfig lotteryConfig, Model model) {
		User user = UserUtils.getUser();
		LotteryConfig temp = lotteryConfigService.getByUserId(user.getId());
		if(temp!=null){
			lotteryConfig = temp;
		}else{
			lotteryConfig.setUser(user);
		}
		
		
		model.addAttribute("lotteryConfig", lotteryConfig);
		return "modules/wechat/lotteryConfigForm";
	}

	@RequiresPermissions("wechat:lotteryConfig:edit")
	@RequestMapping(value = "save")
	public String save(LotteryConfig lotteryConfig, Model model, RedirectAttributes redirectAttributes) {
		User user = UserUtils.getUser();
		if (!beanValidator(model, lotteryConfig)){
			
			return form(lotteryConfig, model);
		}
		lotteryConfigService.save(lotteryConfig);
		addMessage(redirectAttributes, "保存抽奖配置'" + lotteryConfig.getName() + "'成功");
		if(user.isAdmin()){
			return "redirect:"+Global.ADMIN_PATH+"/wechat/lotteryConfig/?repage";
		}else{
			return form(lotteryConfig, model);
		}
	}
	
	@RequiresPermissions("wechat:lotteryConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		lotteryConfigService.delete(id);
		User user = UserUtils.getUser();
		addMessage(redirectAttributes, "删除抽奖配置成功");
		if(user.isAdmin()){
			return "redirect:"+Global.ADMIN_PATH+"/wechat/lotteryConfig/?repage";
		}else{
			return  "modules/wechat/lotteryConfigForm";
		}
	}

}
